package com.iweb.controller;

import com.iweb.entity.User;
import com.iweb.role.UserType;
import com.iweb.service.UserService;
import com.iweb.utitls.GetNumber;
import com.iweb.utitls.Menu;

import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author ryl
 * @version 1.0
 * @data 2025/4/2 14:37
 * 用户操作的入口
 */
public class UserController {
    UserService userService = new UserService();
    private GetNumber getNumber;
    private Scanner scanner;



    public UserController() throws Exception {
        this.getNumber = new GetNumber();
        this.userService = new UserService();
        this.scanner = new Scanner(System.in);
    }

    /**
     * 登录
     */
    public void login() throws Exception {
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        //判断登录
        if (userService.loginUser(username,password)){
            System.out.println("登录成功");
            //获取用户类型
            UserType userType = userService.getUserType(username);
            if (userType == UserType.ADMIN){
                adminSuccessLogin();

            }else if (userType == UserType.USER){
                useerSuccessLogin();
            }else {
                System.out.println("类型错误");
            }
        }else {
            System.out.println("用户名或者密码错误...");
        }
    }

    private void useerSuccessLogin() {
        //TODO:用户成功登录的菜单处理逻辑
        System.out.println("------------------");
        System.out.println("user login success...");
        System.out.println("------------------");
    }

    /**
     * 成功后的菜单处理
     */
    public void adminSuccessLogin() throws Exception {
        while (true) {
            Menu.loginMenu();
            int option = getNumber.getUserInputAsInt();
            switch (option) {
                case 1:
                    addUser();
                    break;
                case 2:
                    deleteUser();
                    break;
                case 3:
                    updateUser();
                    break;
                case 4:
                    selectAllUsers();
                    break;
                case 5:
                    selectUserByName();
                    break;
                case 6:
                    executePageUserSearch();
                    break;
                case 7:
                    System.out.println("返回主菜单成功...");
                    return;
                case 0:
                    System.out.println("退出成功...");
                    return;
                default:
                    System.out.println("无效的选项...");
                    break;
            }
        }
    }

    /**
     *注册
     */
    public void register() throws SQLException {
        System.out.println("请输入要注册的用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入用户对应密码：");
        String password = scanner.nextLine();
        User users = new User(username,password);
        userService.addUser(users);
    }
    private void addUser() throws SQLException {
        System.out.println("请输入要添加的用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入用户对应的密码：");
        String password = scanner.nextLine();
        User users = new User(username, password);
        if (userService.addUser(users)) {
            System.out.println("用户添加成功!!!");
        } else {
            System.out.println("用户已存在...");
        }
    }
    /**
     * 删除用户
     */
    private void deleteUser() throws Exception {
        System.out.println("请输入要删除的用户名：");
        String username = scanner.nextLine();
        userService.deleteUser(username);
    }
    /**
     * 修改密码
     */
    private void updateUser() throws SQLException {
        System.out.println("请输入要修改的用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入用户的新密码：");
        String newPassword = scanner.nextLine();
        User users = new User(username, newPassword);
        userService.updateUser(users);
    }
    /**
     * 查询全部用户
     */
    private void selectAllUsers() {
        userService.selectAllUsers();
    }
    /**
     * 根据名字查询用户
     */
    private void selectUserByName() {
        System.out.println("请输入要查询的用户名：");
        String username = scanner.nextLine();
        userService.selectUser(username);
    }
    /**
     * 分页查询
     */
    private void executePageUserSearch() {
        System.out.println("请输入显示的记录数：");
        int limit = Integer.valueOf(scanner.nextLine());
        System.out.println("请输入查询的偏移量：");
        int offset = Integer.valueOf(scanner.nextLine());
        userService.selectUsersPaged(limit, offset);
    }

}
